package org.summerframework.component.enums.controller;

import org.summerframework.component.enums.service.EnumsService;
import org.summerframework.component.enums.service.dto.EnumsDTO;
import org.summerframework.component.enums.service.dto.EnumsQueryDTO;
import org.summerframework.component.security.controller.BaseController;
import org.summerframework.core.base.result.ResultDTO;
import org.summerframework.core.page.PageResult;
import org.summerframework.core.util.OptionalUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;

@RestController
@Api(tags = "枚举类", description = "功能描述")
@RequestMapping("enums")
public class EnumsController extends BaseController {
    @Resource
    private EnumsService enumsService;

    @PostMapping
    @ApiOperation("添加")
    public ResultDTO<Boolean> insert(EnumsDTO enumsDTO) {
        return ResultDTO.ok(true, enumsService.insert(enumsDTO));
    }

    @PutMapping
    @ApiOperation("更新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", required = true, dataType = "Long", paramType = "query")})
    public ResultDTO<Integer> updateById(EnumsDTO enumsDTO) {
        return ResultDTO.ok(enumsService.updateById(enumsDTO));
    }

    @DeleteMapping("{id}")
    @ApiOperation("根据id删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", required = true, dataType = "Long", paramType = "path")})
    public ResultDTO<Integer> deleteById(@PathVariable("id") Long id) {
        return ResultDTO.ok(enumsService.deleteById(id));
    }

    @DeleteMapping
    @ApiOperation("根据id列表删除删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "根据ids删除", required = true, dataType = "List", paramType = "query")})
    public ResultDTO<Integer> deleteByIds(@RequestParam("ids") List<Long> ids) {
        return ResultDTO.ok(enumsService.deleteByIds(ids));
    }

    @GetMapping("{id}")
    @ApiOperation("根据id查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", required = true, dataType = "Long", paramType = "path")})
    public ResultDTO<EnumsDTO> selectById(@PathVariable("id") Long id) {
        return ResultDTO.ok(OptionalUtils.get(enumsService.selectById(id)));
    }

    @GetMapping
    @ApiOperation("根据query查询")
    public PageResult<EnumsDTO> selectPageResultByQuery(EnumsQueryDTO enumsQueryDTO) {
        return enumsService.selectPageResultByQuery(enumsQueryDTO);
    }

    @GetMapping("kind")
    @ApiOperation("根据kind分组")
    public Map<String, List<EnumsDTO>> selectPageResultByQuery() {
        return enumsService.selectByQuery(EnumsQueryDTO.builder().isPagination(false).build()).stream().collect(Collectors.groupingBy(EnumsDTO::getKind, Collectors.toList()));
    }
}
